﻿@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>PhaAccounts</title>
    @*Easyui需要引入的文件*@
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.5/jquery.min.js"></script>
    <script type="text/javascript" src="~/Content/jquery-easyui-1.4.5/jquery.easyui.min.js"></script>
    <link href="~/Content/jquery-easyui-1.4.5/themes/default/easyui.css" rel="stylesheet" />
    <link href="~/Content/jquery-easyui-1.4.5/themes/icon.css" rel="stylesheet" />
    <script src="~/Scripts/jquery.validate.min.js"></script>
    <script type="text/javascript" src="~/Content/Site.js"></script>
    <script type="text/javascript">
        $(function () {
            //初始化datagrid
            InitGrid();
            InitControl();
            InitDialog();
            InitOriSelectDialog();
            InitOriDialoggrid();
            InitPhaSelectDialog();
            InitPhaDialoggrid();
            $('#btnReload').click(function () {
                $("#grid").datagrid("reload");
            });
            //ajax提交修改表单
            $('#ok').click(function () {
                $('#errdiv').removeClass('dis');
                $('#form').submit();
            });
            $('#cancel').click(function () {
                $('#dd').dialog('close');
            });
            $('#add').click(function () {
                $('#errdiv').attr('class', 'dis');
                $('#form')[0].reset();
                $('#key').val(0);
                $('#editpha').removeAttr("disabled");
                $('#editori').removeAttr("disabled");
                $('#dd').dialog('open');
            });
            $('#editori').click(function () {
                $('#oriselect').dialog('open');
            });
            $('#editpha').click(function () {
                $('#phaselect').dialog('open');
            });
            $('#del').click(function () {
                getAjax("../PHA_Accounts/Del", { phaCode: $('#del_pha').val(), orginID: $('#del_ori').val() }, function (data) {
                    if ($('#del_ori').val() == null || $('#del_pha').val() == "")
                        return false;
                    if (data) {
                        $.messager.alert('提示', '操作成功!');
                        $('#dd').dialog('close');
                        $("#grid").datagrid("reload");
                        return true;
                    }
                    else {
                        $.messager.alert('提示', '操作失败!');
                        return false;
                    }
                });
            });
        });

        //实现对DataGird控件的绑定操作
        function InitGrid() {
            $('#grid').datagrid({
                url: '../PHA_Accounts/LoadList',
                singleSelect: true,
                fit: true,
                nowrap: true,//单行显示
                autoRowHeight: false,
                striped: false,     //斑马纹
                collapsible: true,      //可折叠
                pagination: true,
                pageSize: 20,
                fitColumns: false,       //自适应列宽
                rownumbers: true,
                sortName: 'PhaCode',    //根据某个字段给easyUI排序
                sortOrder: 'asc',
                remoteSort: true,
                loadMsg: '加载中，请等待',
                queryParams: ({
                    pinyin: $('#itemid').val()
                }),  //异步查询的参数
                columns: [[
                     { title: '药品编号', field: 'PhaCode', hidden: true },
                     { title: '药品名称', field: 'PhaName', width: 120, sortable: true },
                     { title: '规格', field: 'Spec', width: 80, sortable: true },
                     { title: '单位', field: 'Unit', width: 60, sortable: true, align: 'center' },
                     { title: '拼音代码', field: 'PinyinCode', width: 70, sortable: true, align: 'center' },
                     { title: '产地', field: 'OrginName', width: 100, sortable: true, align: 'center' },
                     { title: '库存', field: 'Stock', width: 80, sortable: true, align: 'center' },
                     { title: '进货价格', field: 'InWarehousePrice', width: 80, sortable: true, align: 'center' },
                     { title: '批发价格', field: 'WholesalePrice', width: 80, sortable: true, align: 'center' },
                     { title: '零售价格', field: 'RetailPrice', width: 90, sortable: true, align: 'center' },
                     { title: '进货金额', field: 'InWarehouseSum', width: 80, sortable: true, align: 'center' },
                     { title: '批发金额', field: 'WholesaleSum', width: 90, sortable: true, align: 'center' },
                     { title: '零售金额', field: 'RetailSum', width: 90, sortable: true, align: 'center' },
                     { title: '供货单位', field: 'CompanyName', width: 160, sortable: true },
                ]],
                toolbar: '#tb',
                onDblClickRow: function (rowIndex, rowData) {
                    $('#editpha').attr('disabled', 'disabled');
                    $('#editori').attr('disabled', 'disabled');
                    //$('#errdiv').attr('class', 'dis');
                    getAjax("../PHA_Accounts/LoadForm",
                        { phaCode: rowData['PhaCode'], orginID: rowData['OrginID'] }, function (data) {
                            var data = eval("(" + data + ")");
                            SetWebControls(data);
                            $('#key').val(1);
                        });
                    $('#dd').window('open');
                },
                onClickRow: function (index, row) {
                    $('#del_pha').val(row['PhaCode']);
                    $('#del_ori').val(row['OrginID']);
                }

            })
            var p = $('#grid').datagrid('getPager');
            $(p).pagination({
                pageSize: 20,
                pageList: [10, 12, 20, 40, 60, 100, 200],
                beforePageText: '第',
                afterPageText: '页   共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
        };

        function InitDialog() {
            $('#dd').dialog({
                title: '修改信息',
                width: 740,
                height: 220,
                top: ($(window).height() - 220) * 0.5,      //居中
                left: ($(window).width() - 740) * 0.5,
                closed: true,
                cache: false,
                modal: true,
                buttons: '#dlg-buttons'
            });
        }

        //产地选择器
        function InitOriDialoggrid() {
            $('#origrid').datagrid({
                url: '../PHA_Orgin/LoadList',
                height: 410,
                nowrap: true,//单行显示
                autoRowHeight: false,
                striped: false,     //斑马纹
                collapsible: true,      //可折叠
                singleSelect: true,
                pagination: true,
                sortName: 'OrginID',    //根据某个字段给easyUI排序
                sortOrder: 'asc',
                pageSize: 20,
                fitColumns: true,       //自适应列宽
                rownumbers: true,
                toolbar: '#origrid_tb',
                queryParams: ({
                    query: $('#origrid_dir').val()
                }),
                columns: [[
                     { title: '编号', field: 'OrginID', hidden: true },
                     { title: '产地', field: 'OrginName' },
                     { title: '拼音代码', field: 'PinyinCode' },
                     { title: '生产厂家', field: 'Manufacturer' },
                ]],
                onDblClickRow: function (rowIndex, rowData) {
                    $('#OrginName').val(rowData['OrginName']);
                    $('#OrginID').val(rowData['OrginID']);
                    $('#oriselect').dialog('close');

                }

            });
            var p = $('#origrid').datagrid('getPager');
            $(p).pagination({
                pageSize: 20,
                pageList: [10, 12, 20, 40, 60, 100, 200],
                beforePageText: '第',
                afterPageText: '页   共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
        }

        function InitOriSelectDialog() {
            $('#oriselect').dialog({
                title: '选择产地',
                width: 400,
                height: 450,
                top: ($(window).height() - 450) * 0.5,      //居中
                left: ($(window).width() - 400) * 0.5,
                closed: true,
                cache: false,
                modal: true,
            });
        }


        //药品选择器
        function InitPhaDialoggrid() {
            $('#phagrid').datagrid({
                url: '../DrugManagement/LoadList',
                height: 410,
                nowrap: true,//单行显示
                autoRowHeight: false,
                singleSelect: true,
                striped: false,     //斑马纹
                collapsible: true,      //可折叠
                pagination: true,
                pageSize: 20,
                fitColumns: true,       //自适应列宽
                rownumbers: true,
                sortName: 'PhaCode',    //根据某个字段给easyUI排序
                sortOrder: 'asc',
                remoteSort: true,
                loadMsg: '加载中，请等待',
                queryParams: ({
                    query: $('#phagrid_dir').val()
                }),  //异步查询的参数
                columns: [[
                     { title: '药品编号', field: 'PhaCode', hidden: true },
                     { title: '药品名称', field: 'PhaName', width: 120, sortable: true },
                     { title: '药品规格', field: 'Spec', width: 80, sortable: true },
                     { title: '药品单位', field: 'Unit', width: 60, sortable: true, align: 'center' },
                     { title: '拼音码', field: 'PinyinCode', width: 90 }
                ]],
                toolbar: '#phagrid_tb',
                onDblClickRow: function (rowIndex, rowData) {
                    $('#PhaCode').val(rowData['PhaCode']);
                    $('#PhaName').val(rowData['PhaName']);
                    $('#Spec').val(rowData['Spec']);
                    $('#Unit').val(rowData['Unit']);
                    $('#PinyinCode').val(rowData['PinyinCode']);
                    $('#phaselect').dialog('close');
                }
            })
            var p = $('#phagrid').datagrid('getPager');
            $(p).pagination({
                pageSize: 20,
                pageList: [10, 12, 20, 40, 60, 100, 200],
                beforePageText: '第',
                afterPageText: '页   共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
        };
        function InitPhaSelectDialog() {
            $('#phaselect').dialog({
                title: '选择药品',
                width: 500,
                height: 450,
                top: ($(window).height() - 450) * 0.5,      //居中
                left: ($(window).width() - 500) * 0.5,
                closed: true,
                cache: false,
                modal: true,
            });
        }


        //绑定供货单位下拉框
        function InitControl() {
            var dataJson = "";
            getAjax("/PHA_Accounts/LoadSelectComPany", null, function (data) {
                datajson = eval("(" + data + ")");
                $.each(datajson, function (i) {
                    $("#CompanyID").append($("<option></option>").val(datajson[i].CompanyID).html(datajson[i].CompanyName));
                });
            })
        }

    </script>
    <script>
        //表单验证和提交
        $(function () {
            $('#form').validate({
                rules: {
                    PhaCode: {
                        required: true
                    },
                    OrginName: {
                        required: true
                    },
                    Stock: {
                        required: true
                    },
                    CompanyID: {
                        required: true
                    },
                    InWarehousePrice: {
                        required: true
                    },
                    RetailPrice: {
                        required: true
                    },
                    WholesalePrice: {
                        required: true
                    }
                },
                messages: {
                    PhaCode: "药品序号不能为空！",
                    OrginName: "药品产地不能为空！",
                    Stock: "库存不能为空！",
                    CompanyID: "进货单位不能为空！",
                    InWarehousePrice: "入库价格不能为空！",
                    RetailPrice: "零售价格不能为空！",
                    WholesalePrice: "批发价格不能为空！"
                },
                errorLabelContainer: '#errdiv>ul',
                wrapper: 'li',
                submitHandler: function (form) {
                    var postData = GetWebControls("#form");
                    getAjax("/PHA_Accounts/AcceptClick?key=" + $('#key').val(),
                        postData, function (data) {
                            if (data) {
                                //此处可以优化，提示消息可更美观。
                                $.messager.alert('提示', '操作成功!');
                                $('#dd').dialog('close');
                                $("#grid").datagrid("reload");
                                return true;
                            }
                            else if (data == -1) {
                                $.messager.alert('提示', '已存在该记录或单号重复，请重新填写！');
                                return false;
                            }
                            else {
                                $.messager.alert('提示', '操作失败!');
                                return false;
                            }
                        });
                },
                invalidHandler: function (form, validator) {  //不通过回调
                    return false;
                }
            });
        });
    </script>
    <style>
        .dis {
            display: none;
        }

        fieldset {
            border-color: #E0ECFF;
        }

        .tb-line {
            margin-bottom: 5px;
        }

        .tb-col-200 {
            width: 200px;
        }

        .tb-col-160 {
            width: 160px;
        }

        .tb-col-140 {
            width: 140px;
        }

        .tb-col-120 {
            width: 120px;
        }

        .tb-col-80 {
            width: 80px;
        }

        .tb-col-40 {
            width: 40px;
        }
    </style>
</head>
<body style="margin:2px;padding:0">
    <div>
        <div style="position:fixed;width:100%;height:100%">
            <table id="grid"></table>
        </div>
        <input id="del_pha" type="hidden" name="del_pha" value="" />
        <input id="del_ori" type="hidden" name="del_ori" value="" />
        <div id="tb" style="padding:3px">
            <a id="add" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-add'">新增</a>
            <a id="del" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-remove'">删除</a>
            <a id="btnReload" href="#" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-reload'">刷新</a>
            <span>拼音码:</span>
            <input id="itemid" style="border:1px solid #ccc">
            <a href="#" class="easyui-linkbutton" plain="true" onclick="InitGrid()">查找</a>
        </div>
    </div>

    <div id="dd" class="easyui-dialog" style="position:relative">
        <form id="form" name="form" method="post">
            <input id="key" type="hidden" value="" />
            <div style="margin:10px">
                <div class="tb-line">
                    <span>药品编码：</span><input type="text" id="PhaCode" class="tb-col-120" name="PhaCode" value="" disabled="disabled" />
                    <input type="button" id="editpha" value="改" />
                    <span>药品名：</span><input type="text" id="PhaName" class="tb-col-160" name="PhaName" value="" disabled="disabled" />
                    <span>规格：</span><input type="text" id="Spec" name="Spec" class="tb-col-80" value="" disabled="disabled" />
                    <span>单位：</span><input type="text" id="Unit" name="Unit" class="tb-col-40" value="" disabled="disabled" />

                </div>
                <div class="tb-line">
                    <span>拼音：</span><input type="text" id="PinyinCode" name="PinyinCode" class="tb-col-80" value="" disabled="disabled" />
                    <span>产地：</span><input type="text" id="OrginName" name="OrginName" class="tb-col-120" value="" disabled="disabled" />
                    <input type="hidden" id="OrginID" />
                    <input type="button" id="editori" value="改" />
                    <span>库存：</span><input type="text" id="Stock" name="Stock" class="tb-col-40" value="" />
                    <span>供货单位：</span><select id="CompanyID" name="CompanyID" class="tb-col-200"></select>
                </div>

                <div class="tb-line">
                    <span>进货价格：</span><input type="text" id="InWarehousePrice" name="InWarehousePrice" class="tb-col-120" value="" />
                    <span>批发价格：</span><input type="text" id="WholesalePrice" name="WholesalePrice" class="tb-col-120" value="" />
                    <span>零售价格：</span><input type="text" id="RetailPrice" name="RetailPrice" class="tb-col-120" value="" />
                </div>
                <div class="tb-line">
                    <span>进货金额：</span><input type="text" id="InWarehouseSum" name="InWarehouseSum" class="tb-col-120" value="" disabled="disabled" />
                    <span>批发金额：</span><input type="text" id="WholesaleSum" name="WholesaleSum" class="tb-col-120" value="" disabled="disabled" />
                    <span>零售金额：</span><input type="text" id="RetailSum" name="RetailSum" class="tb-col-120" value="" disabled="disabled" />
                </div>
            </div>
            <div class="errdiv" id="errdiv">
                <ul></ul>
            </div>
            <div id="dlg-buttons">
                <a id="ok" href="#" class="easyui-linkbutton" iconcls="icon-ok">确定</a>
                <a id="cancel" href="#" class="easyui-linkbutton" iconcls="icon-cancel">取消</a>
            </div>
        </form>
    </div>

    <div id="oriselect" class="easyui-dialog" style="position:relative">
        <table id="origrid"></table>
        <div id="origrid_tb" style="padding:3px">
            <span>拼音码:</span>
            <input id="origrid_dir" style="border:1px solid #ccc">
            <a href="#" class="easyui-linkbutton" plain="true" onclick="InitOriDialoggrid()">查找</a>
        </div>
    </div>
    <div id="phaselect" class="easyui-dialog" style="position:relative">
        <table id="phagrid"></table>
        <div id="phagrid_tb" style="padding:3px">
            <span>拼音码:</span>
            <input id="phagrid_dir" style="border:1px solid #ccc">
            <a href="#" class="easyui-linkbutton" plain="true" onclick="InitPhaDialoggrid()">查找</a>
        </div>
    </div>
</body>
</html>
